﻿using ConfExpert.Forms.Base;
using DevTeam;
using SpreadsheetGear;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Lojistik.CafeCoton
{
    public partial class Barkod : XForm
    {
        int BrandId;
        public Barkod(int brandId)
        {
            InitializeComponent();
            BrandId = brandId;
        }

        private void Barkod_Load(object sender, EventArgs e)
        {
            
            //splitContainerControl1.SizeChanged+=MySizeChanged;
            DataSet d = ExecuteDataSet("GetCCBarkod", BrandId, 1);
            gridView1.Init(d.Tables[0]);
            gridView2.Init(d.Tables[1]);

            gridView1.RowCellStyle += gridView1_RowCellStyle;

            gridView1.MakeReadOnly();
            gridView1.FormatAsNumber(0, "Toplam");
            gridView1.AddSummary("Toplam");
            gridView1.BestFitColumns();

            gridView2.MakeReadOnly();
            gridView2.MakeColumnInvisible("DN");
            gridView2.FormatAsNumber(0, "Qty");
            gridView2.BestFitColumns();
            DoFilter();

            gridView1.SelectionChanged += gridView1_SelectionChanged;
            bbiOzet.ItemClick += bbiOzet_ItemClick;
            bbiExport.ItemClick += bbiExport_ItemClick;

            //GridManager gm = GridManager.GetManager(gridView1);
            //gm.GridMenu.AddMenu("Barkodlar yazdırıldı", Printed);

        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
             if (e.RowHandle > -1)
                 if (e.Column.FieldName == "DosyaNo")
                     e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void bbiExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridView2.EndEditing();
            string DNs = gridView1.GetSelectedValues("DosyaNo");
            
            ExecuteNonQuery("SetBarcodeIsPrinted2", DNs);
            gridView2.ExportToExcel();

            foreach (string DN in DNs.Split(",".ToCharArray()))
                gridView1.DataTable().Select("DosyaNo='" + DN + "'")[0]["isPrinted"] = true;
            gridView1.RefreshData();
        }

        void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            DoFilter();
        }

        /*
        private void Printed(object sender, EventArgs e)
        {
            string DNs = gridView1.GetSelectedValues("DosyaNo");

            ExecQuery("SetBarcodeIsPrinted", DNs);
            int rowhandle = gridView1.FocusedRowHandle;
            gridView1.VeriyiGuncelle("GetCcBarkod", BrandId, 1);
            gridView1.FocusedRowHandle = rowhandle;
        }

        private string GetIdx()
        {
            string idxs = "";
            foreach (int rowHandle in gridView1.GetSelectedRows())
                if (rowHandle > -1)
                    idxs += gridView1.GetRowCellValue(rowHandle, "idx").ToString() + ",";


            idxs = idxs.RemoveIfEndsWith(",");

            return idxs;
        }
        */

        void DoFilter()
        {
            string DNs = gridView1.GetSelectedValues("DosyaNo").Replace(",", "','");
            gridView2.ActiveFilterString = "DN in ('" + DNs + "')";
            gridView2.BestFitColumns();
        }

        void bbiOzet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView2.EndEditing();
            gridView1.EndEditing();
            string DNs = gridView1.GetSelectedValues("DosyaNo");
            DataSet ds = ExecuteDataSet("GetCcBarkodOzet", BrandId, 0, DNs);

            SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
            SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets[0];
            SpreadsheetGear.IRange cells = workbook.Worksheets[0].Cells;

            int row = 0;
            foreach (DataTable dt in ds.Tables)
            {
                cells[row++, 1].RowHeight = 28;

                //header
                cells[row, 0].Value = "MARKA";
                cells[row + 1, 0].Value = "DOSYA NO";
                cells[row + 2, 0].Value = "MODEL";

                cells[row, 1].Value = "Cafe Cotton";
                cells[row + 1, 1].Value = dt.Rows[0]["DosyaNo"].ToString();
                cells[row + 2, 1].Value = dt.Rows[0]["Model"].ToString();

                string cl = ((char)(dt.Columns.Count + 62)).ToString();
                cells["A" + (row + 1) + ":" + cl + (row + 4).ToString()].Font.Bold = true;

                //beden isimleri
                int i = 0;
                foreach (DataColumn dtColumn in dt.Columns)  // column isimlerini set et
                {
                    switch (dtColumn.ColumnName)
                    {
                        case "Model":
                        case "DosyaNo":
                            break;

                        default:
                            cells[row + 3, i++].Value = dtColumn.ToString();    //.Replace("T-", string.Empty);
                            break;
                    }
                }

                //formatlama
                for (int h = 0; h < 4; h++)
                {
                    if (h < 3)
                        cells["B" + (row + 1) + ":" + cl + (row + 1)].MergeCells = true;
                    cells["B" + (row + 1) + ":" + cl + (row + 1)].Style.HorizontalAlignment = SpreadsheetGear.HAlign.Center;

                    row++;
                }

                cells["A" + row + ":" + cl + row].Interior.Color = SpreadsheetGear.Color.FromArgb(220, 220, 220);

                //adetler
                foreach (DataRow dtRow in dt.Rows)
                {
                    i = 0;
                    foreach (DataColumn dtColumn in dt.Columns)  // column isimlerini set et
                    {
                        switch (dtColumn.ColumnName)
                        {
                            case "Model":
                            case "DosyaNo":
                                break;

                            default:
                                cells[row, i++].Value = dtRow[dtColumn].ToString();
                                break;
                        }
                    }

                    //satırın toplamı
                    cells[row, i++].Formula = "=SUM(B" + (row + 1) + ":" + cl + (row + 1).ToString() + ")";
                    //cells[row, (i - 1)].Font.Italic = true;
                    cells[row, (i - 1)].Font.Color = SpreadsheetGear.Color.FromArgb(70, 130, 180);
                    row++;

                }

                //sütunların toplamı
                i = 1;
                foreach (DataColumn dtColumn in dt.Columns)  // column isimlerini set et
                {
                    switch (dtColumn.ColumnName)
                    {
                        case "Model":
                        case "DosyaNo":
                            break;

                        default:
                            string GT = ((char)(i + 65)).ToString();
                            cells[row, i++].Formula = "=SUM(" + GT + (row - dt.Rows.Count + 1) + ":" + GT + row.ToString() + ")";
                            break;
                    }
                }

                //son formatlama
                //cells[row, 0].EntireRow.Font.Italic = true;
                cells[row, 0].EntireRow.Font.Color = SpreadsheetGear.Color.FromArgb(70, 130, 180);

                IRange r = cells["A" + (row - dt.Rows.Count - 3) + ":" + cl + (row)];
                r.Borders.Weight = SpreadsheetGear.BorderWeight.Hairline;
                
                r.Borders[SpreadsheetGear.BordersIndex.EdgeTop].Weight = 
                r.Borders[SpreadsheetGear.BordersIndex.EdgeBottom].Weight = 
                r.Borders[SpreadsheetGear.BordersIndex.EdgeLeft].Weight = 
                r.Borders[SpreadsheetGear.BordersIndex.EdgeRight].Weight = SpreadsheetGear.BorderWeight.Thin;

                cells["A" + (row - dt.Rows.Count) + ":" + cl + (row - dt.Rows.Count)].Borders[SpreadsheetGear.BordersIndex.EdgeBottom].Weight = SpreadsheetGear.BorderWeight.Thin;

                row++;
                cells[row, 1].EntireRow.PageBreak = SpreadsheetGear.PageBreak.Manual;
            }

            workbook.Worksheets[0].UsedRange.Columns.AutoFit();

            workbook.Worksheets[0].PageSetup.CenterVertically = true;
            workbook.Worksheets[0].PageSetup.CenterHorizontally = true;
            string colx = ((char)(workbook.Worksheets[0].UsedRange.ColumnCount + 64)).ToString();
            cells["B1:" + colx + "1"].ColumnWidth = 5;

            //pagebreak;

            string FileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache) + "\\Export." + (x++).ToString() + ".xlsx";
            workbook.SaveAs(FileName, SpreadsheetGear.FileFormat.OpenXMLWorkbook);
            DevTeam.FileManager.RunFile(FileName);
        }

        int x = 0;
    }
}
